﻿//	获取样式
function getStyle(obj, name) {
	if(obj.currentStyle) {
		if(name == "opacity") {
			return Math.round(parseFloat(obj.currentStyle[name]) * 100);
		} else {
			return parseInt(obj.currentStyle[name]);
		}
	} else {
		if(name == "opacity") {
			return Math.round(parseFloat(getComputedStyle(obj, false)[name]) * 100);
		} else {
			return parseInt(getComputedStyle(obj, false)[name]);
		}
	}
}

//	添加class
function addClass(obj, addClassName) {
	//	获取当前class名
	let objClassName = obj.className;
	
	//	把class名在空格的地方拆分成数组
	let arrClassName = objClassName.split(" ");
	
	//	把需要添加的class以空格拆分成数组
	let arraddClassName = addClassName.split(" ");
	
	//	把两个数组拼接
	let splicearr = arrClassName.concat(arraddClassName);
	
	//	把数组以空格分割成字符串
	let ClassName = splicearr.join(" ");
	
	//	把class添加给对象
	obj.className = ClassName;
}
	
//	删除class
function removeClass(obj, removeClassName) {
	//	获取当前class名
	let objClassName = obj.className;
	
	//	把class名在空格的地方拆分成数组
	let arrClassName = objClassName.split(" ");
	
	//	把需要删除的class以空格拆分成数组
	let arrremoveClassName = removeClassName.split(" ");
	
	//	循环class数组
	for (let i = 0; i < arrClassName.length; i++){
		//	判断当前下标的class名是否和需要删除的相同
		for (let j = 0; j < arrremoveClassName.length; j++) {
			if (arrClassName[i] == arrremoveClassName[j]) {
				//	如果相同删除第当前下标的一个数组
				arrClassName.splice(i, 1);
			}
		}
	}
	
	//	把数组以空格分割成字符串
	let ClassName = arrClassName.join(" ");
	
	//	把class添加给对象
	obj.className = ClassName;
}

//	获取class
function getByClass(oParent, sClass) {
	let aEle = oParent.getElementsByTagName("*");
	let aResult = [];

	for(let i = 0; i < aEle.length; i++) {
		if(aEle[i].className == sClass) {
			aResult.push(aEle[i]);
		}
	}

	return aResult;
}

//	运动框架
function startMove(obj, json, fnEnd) {
	clearInterval(obj.timer);

	obj.timer = setInterval(function() {
		let bStop = true;

		for(let attr in json) {
			let cur = getStyle(obj, attr);
			let speed = (json[attr] - cur) / 6;
			speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);

			if(cur != json[attr]) {
				bStop = false;
			}

			if(attr == "opacity") {
				obj.style.filter = "alpha(opacity = " + (cur + speed) + ")";
				obj.style[attr] = (cur + speed) / 100;
			} else {
				obj.style[attr] = cur + speed + "px";
			}
		}

		if(bStop) {
			clearInterval(obj.timer);

			if(fnEnd) fnEnd();
		}
	}, 30);
}

//	获取鼠标坐标
function getPos(ev) {
	let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
	let scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;

	return {
		x: ev.clientX + scrollLeft + "px",
		y: ev.clientY + scrollTop + "px"
	};
}

//	事件绑定
function myAddEvent(obj, ev, fn) {
	if(obj.attachEvent) {
		obj.attachEvent("on" + ev, fn);
	} else {
		obj.addEventListener(ev, fn, false);
	}
}

//	封装ajax
function ajax(type, url, success, error) {
	//	1.创建ajax对象兼容
	if(window.XMLHttpRequest) {
		let oAjax = new XMLHttpRequest();
	} else {
		let oAjax = new ActiveXObject("Microsoft.XMLHTTP");
	}

	//	2.链接服务器
	oAjax.open(type, url + "?time=" + new Date().getTime(), true);

	//	3.发送请求
	oAjax.send();

	//	4.接收返回数据
	oAjax.onreadystatechange = function() {
		if(oAjax.readyState == 4) {	//	读取完成
			if(oAjax.status == 200) { //	成功
				success(oAjax.responseText);	//	作为参数传递给它
			} else {	//	失败
				if (error) {	//	只有传入参数才调用
					error(oAjax.status);	//	作为参数传递给它
				}
			}
		}
	}
}

//	封装设置cookie
function setCookie(name, value, iDay) {
	let oDate = new Date();
	
	oDate.setDate(oDate.getDate() + iDay);
	
	document.cookie = name + "=" + value + ";expires=" + oDate;
}

//	封装读取cookie
function getCookie(name) {
	let arr1 = document.cookie.split("; ");
	
	for (let i = 0; i < arr1.length; i++) {
		let arr2 = arr1[i].split("=");
		
		if(arr2[0] == name) {
			return arr2[1];
		}
	}
	
	return "";
}

//	封装删除cookie
function removeCookie(name) {
	let oDate = new Date();
	
	oDate.setDate(oDate.getDate() - 1);
	
	document.cookie = name + "=;expires=" + oDate;
}

//	封装清空cookie
function emptyCookie() {
	let arr1 = document.cookie.split("; ");
	
	let oDate = new Date();
	
	oDate.setDate(oDate.getDate() - 1);
	
	for (let i = 0; i < arr1.length; i++) {
		let arr2 = arr1[i].split("=");
		
		document.cookie = arr2[0] + "=;expires=" + oDate;
	}
}

//	本地预览图片，只支持ie10+
function fileReader(file, obj) {
	let file = file.files[0];

	if(!file) return;
	
	let reader = new FileReader();
	
	reader.readAsDataURL(file);
	
	reader.onload = function (event) {
		// let oEvent = event || window.event;
		let oImg = document.getElementById(obj);
		oImg.src = this.result;
	}
}

//	获取表单全部数据
function getAllFormData(form) {
	let data = decodeURIComponent(form.serialize(), true);
	let arrs = data.split("&");
	let item = null,
		key = null,
		value = null,
		obj = {};
	arrs.forEach(function (arr, index){
		item = arr.split("=");
		key = item[0];
		value = item[1];
		if(key.indexOf("[]") == -1) {
			obj[key] = value;
		}else {
			key = key.replace("[]", "");
			!newData[key] ? newData[key] = [] : "";
			newData[key].push(value);
		}
	})
	return obj;
}

//	时间戳转换
function timestampFormat( timestamp ) {
        function zeroize( num ) {
            return (String(num).length == 1 ? '0' : '') + num;
        }

        var curTimestamp = parseInt(new Date().getTime() / 1000); //当前时间戳
        var timestampDiff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数

        var curDate = new Date( curTimestamp * 1000 ); // 当前时间日期对象
        var tmDate = new Date( timestamp * 1000 );  // 参数时间戳转换成的日期对象

        var Y = tmDate.getFullYear(), m = tmDate.getMonth() + 1, d = tmDate.getDate();
        var H = tmDate.getHours(), i = tmDate.getMinutes(), s = tmDate.getSeconds();

        if ( timestampDiff < 60 ) { // 一分钟以内
            return "刚刚";
        } else if( timestampDiff < 3600 ) { // 一小时前之内
            return Math.floor( timestampDiff / 60 ) + "分钟前";
        } else if ( curDate.getFullYear() == Y && curDate.getMonth()+1 == m && curDate.getDate() == d ) {   // 今天
            return zeroize(H) + ':' + zeroize(i);
        } else {
            var newDate = new Date( (curTimestamp - 86400) * 1000 ); // 参数中的时间戳加一天转换成的日期对象
            if ( newDate.getFullYear() == Y && newDate.getMonth()+1 == m && newDate.getDate() == d ) {  // 昨天
                return '昨天' + zeroize(H) + ':' + zeroize(i);
            } else if ( curDate.getFullYear() == Y ) {  // 今年
                return  zeroize(m) + '月' + zeroize(d) + '日 ' + zeroize(H) + ':' + zeroize(i);
            } else {    // 往年
                return  Y + '年' + zeroize(m) + '月' + zeroize(d) + '日 ' + zeroize(H) + ':' + zeroize(i);
            }
        }
    }